﻿// FogBugz Community Case List Plugin
// ----------------------------------
// 
// Copyright 2010, 2011 by Sirius Technologies AG / Arsène von Wyss - avw@gmx.ch
// 
// Development has been sponsered by Sirius Technologies AG, Basel
// 
// Source:
// 
// https://communitycaselist.googlecode.com/hg/
// 
// License:
// 
// The library is distributed under the GNU Lesser General Public License:
// http://www.gnu.org/licenses/lgpl.html
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Web;
using System.Xml;

using FogCreek.FogBugz;
using FogCreek.FogBugz.Exception;
using FogCreek.FogBugz.Globalization;
using FogCreek.FogBugz.Plugins.Entity;
using FogCreek.FogBugz.UI;
using FogCreek.FogBugz.UI.Dialog;

using Sirius.FogBugz;

namespace Sirius.CommunityCaseList {
	partial class CommunityCaseList {
		private CBugDisplayDialogItem[] GetTicketDialogItem(CBug[] rgbug, bool edit) {
			try {
				CPerson currentPerson = api.Person.GetCurrentPerson();
				if (currentPerson.GetPermissionLevel() >= PermissionLevel.Normal) {
					int withTicket = 0;
					int withoutTicket = 0;
					bool canEdit = false;
					foreach (CBug bug in rgbug) {
						if (IsValidTicket(bug.sTicket)) {
							withTicket++;
						} else {
							withoutTicket++;
						}
						canEdit |= bug.IsWritableByPerson(currentPerson.ixPerson) || (bug.ixBug <= 0);
					}
					CBugDisplayDialogItem item = new CBugDisplayDialogItem("ticket");
					item.sLabel = "Visibility";
					if (edit && canEdit) {
						string[] optionNames;
						string[] optionValues;
						int preselect;
						if (rgbug.Length == 1) {
							optionNames = new[] {"Private", "Public"};
							optionValues = new[] {"private", "public"};
							preselect = Math.Sign(withTicket);
						} else {
							optionNames = new[] {string.Format("-- {0} --", CLocalizer.Current.FB_NO_CHANGE), "Private", "Public"};
							optionValues = new[] {"", "private", "public"};
							preselect = (withTicket == 0) ? 1 : (withoutTicket == 0) ? 2 : 0;
						}
						item.sContent = Forms.SelectInput(api.AddPluginPrefix("ticket"), optionNames, optionValues[preselect], optionValues);
						item.sInstructions = "Controls the ticket used for public bug viewing. Note: Removing a ticket will render existing tickets to this case obsolete even if a ticket is re-created later.";
					} else {
						if (withTicket == 0) {
							item.sContent = "Private";
						} else if (withoutTicket == 0) {
							if (rgbug.Length == 1) {
								item.sContent = string.Format("<a href=\"default.asp?{0}\">Public</a>", ExtractShortTicket(rgbug[0].sTicket));
							} else {
								item.sContent = "Public";
							}
						} else {
							item.sContent = CLocalizer.Current.FB_VARIOUS;
						}
					}
					return new[] {item};
				}
			} catch (FogBugzSecurityException) {
				// in case of a security problem, just skip this operation
			}
			return new CBugDisplayDialogItem[0];
		}

		public CBugDisplayDialogItem[] BugDisplayViewLeft(CBug[] rgbug, bool fPublic) {
			if (rgbug.Length > 0) {
				if (api.Person.GetCurrentPerson().fCommunity) {
					int ixProject;
					if (int.TryParse(api.Request[api.AddPluginPrefix("ixProject")], out ixProject)) {
						rgbug[0].IgnorePermissions = true;
						CBugDisplayDialogItem item = new CBugDisplayDialogItem("communityCaseView");
						item.sLabel = CLocalizer.Current.FB_PROJECT;
						string htmlToInsert = string.Format(@"<div class=""content subtitle""><a href=""{0}#{1}"">{2}: {3}</a> <span class=""fixfor"">| {4}: {5}</span></div>", PluginPageLinkBuilder.ForPage(api).Append("ixProject", XmlConvert.ToString(rgbug[0].ixProject)), XmlConvert.ToString(rgbug[0].ixFixFor),
						                                    GetHtmlStringOrEmpty(rgbug[0].QueryField("sProject")), GetHtmlStringOrEmpty(rgbug[0].QueryField("sArea")), HttpUtility.HtmlEncode(CLocalizer.Current.FB_FIX_FOR), GetHtmlStringOrEmpty(rgbug[0].QueryField("sFixFor")));
						item.sContent = string.Format(@"<script language=""JavaScript"" type=""text/javascript"">$('#mainArea div.bugTip').remove();$('#statusbarspacer').before('{0}');</script>", htmlToInsert.Replace("\"", "\\\"").Replace("'", "\\'"));
						rgbug[0].IgnorePermissions = false;
						return new[] {item};
					}
				} else {
					return GetTicketDialogItem(rgbug, false);
				}
			}
			return new CBugDisplayDialogItem[0];
		}

		public CBugDisplayDialogItem[] BugDisplayViewTop(CBug[] rgbug, bool fPublic) {
			return new CBugDisplayDialogItem[0];
		}

		public CBugDisplayDialogItem[] BugDisplayEditLeft(CBug[] rgbug, BugEditMode nMode, bool fPublic) {
			if (!fPublic) {
				return GetTicketDialogItem(rgbug, true);
			}
			return new CBugDisplayDialogItem[0];
		}

		public CBugDisplayDialogItem[] BugDisplayEditTop(CBug[] rgbug, BugEditMode nMode, bool fPublic) {
			return new CBugDisplayDialogItem[0];
		}
	}
}
